<?php


namespace api\zwt\controller;


use api\zwt\validate\UserValidate;
use cmf\controller\RestBaseController;
use api\zwt\model\UserModel;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class UserController extends RestBaseController
{
    //验证码
    public function sendCode()
    {
        $mobile = input('mobile');
        //验证验证码
        $code = sendAliSms($mobile);
        if ($code['code'] == 1) {
            $this->success('发送成功');
        } else {
            $this->error($code['msg']);
        }
    }
    //注册
    public function register()
    {
        //接收数据
        $data = input('post.');

        //验证器
        $validate = new UserValidate();
        $res = $validate->scene('register')->check($data);
        if (!$res) {
            return error($validate->getError());
        }
        //查重
        $repeat = UserModel::where('mobile', '=', $data['mobile'])->find();
        if ($repeat) {
            return error('手机号已注册');
        }
        //判断手机号是否被注册
        $user = UserModel::where('mobile', $data['mobile'])->find();
        if ($user) {
            return error('手机号被注册');
        }
        //检验验证码
        $check = checkSmsCode($data['mobile'], $data['code']);
        if (!$check) {
            return error('验证码错误');
        }
        //入库
        if (UserModel::create($data)) {
            return success('注册成功');
        } else {
            return error('注册失败');
        }

    }
    //登录
    public function login(){
        $data = input('post.');
        //验证器
        $validate = new UserValidate();
        $res = $validate->scene('login')->check($data);
        if(!$res){
            return error($validate->getError());
        }
        //查看用户是否存在
        $user = UserModel::where('mobile','=',$data['mobile'])->find();
        $check = checkSmsCode($data['mobile'],$data['code']);
        if(!$check){
            $this->error('验证码错误');
        }
        if(!$user){
            return error('该用户不存在');
        }
        return success('登陆成功',[
            //JWT
            'token' => signToken($user['id'],$user['mobile']),
        ]);

    }


//    public function List()
//    {
//        //获取搜索字段
//        $key = input('key');
//        $page = input('page/d',1);
//        $pageSize = input('page_size/d',10);
//        $where = [];
//        //判断是否有值
//        if($key){
//            $where[] = ['name', 'like', '%' . $key . '%'];
//        }
//        $list = UserModel::field('id,name,image,mobile')
//            ->where($where)
//            ->page($page,$pageSize)
//            ->select();
//        $this->success('个人信息',$list);
//    }

    //修改个人信息
    public function UserEdit()
    {
        $header = request()->header();
        if(!isset($header['token'])||empty($header['token']))
        {
            $this->error('缺少token');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        $res = JWT::decode($header['token'],$key);
        try {
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        $data = input('post.');
        $user = UserModel::find($res->data->id);
        $user-> name = $data['name'];
        $user-> image = $data['image'];
        $user-> mobile = $data['mobile'];
        if($user->save())
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
   }

   public function getUserInfo()
   {
       $header = request()->header();
       //halt($header);
        if(!isset($header['token'])||empty($header['token']))
        {
            $this->error('缺少token');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        $res = JWT::decode($header['token'],$key);
       try {
           $res = JWT::decode($header['token'],$key);
       }catch (\Exception $exception) {
           $this->error($exception->getMessage());
       }
       //halt($res->data->id);
       $userInfo = UserModel::field('id,name,image,mobile')
           ->where('id',$res->data->id)
           ->find();
       $userInfo['image'] = request()->domain() . 'storage/' . $userInfo['image'];
       $this->success('用户信息',$userInfo);
   }


}
